package interfaceGrafica;

import java.awt.Color;
import java.util.ArrayList;
import java.util.List;
import javax.swing.GroupLayout;
import javax.swing.JOptionPane;
import javax.swing.LayoutStyle;
import logica.main.MainGrafico;
import logica.pessoa.ClienteJuridico;
import logica.util.TipoDeCadastro;
import logica.util.TipoDeEntradaInvalida;
import persistencia.GeradorDeLogs;
import persistencia.MecanismoDePersistenciaSeguro;
import util.Constantes;
import util.Endereco;
import util.TipoDeLogradouro;
import util.UF;


@SuppressWarnings("serial")
public class InterfaceClienteJuridico extends javax.swing.JPanel {

    /**
	 * 
	 */
	
	private List<ClienteJuridico> clientes;
    private ClienteJuridico clienteAtual;
    private static MecanismoDePersistenciaSeguro mecanismoDePersistenciaClientesJuridico;
    private static GeradorDeLogs geradorDeLog;

    public InterfaceClienteJuridico() {
            try {
                geradorDeLog = new GeradorDeLogs();
            } catch (Exception e) {
            }
            initComponents();
            lerDados();
            
            try {
				geradorDeLog.escreveLog("O usuario  "+MainGrafico.getUsuarioLogado().getLogin()+" entrou na area de gereciamento de Clientes Juridicos.");
			} catch (Exception e) {
				
			}

            for (UF estado : UF.values()) {
                jComboBox2.addItem(estado);
            }
            for (TipoDeLogradouro tipoDeLogradouro : TipoDeLogradouro.values()) {
                jComboBox1.addItem(tipoDeLogradouro);
            }

            for (ClienteJuridico cliente : clientes) {
                list1.add(cliente.getNomeFantasia() + " # " + cliente.getCNPJ());
            }
            jButton5.setEnabled(false);
            jButton3.setEnabled(false);
            this.setVisible(true);
    }

    @SuppressWarnings("unchecked")
	private void lerDados(){
        try{
            mecanismoDePersistenciaClientesJuridico = new MecanismoDePersistenciaSeguro("arquivos/clientejuridico.ser");
            clientes = (ArrayList<ClienteJuridico>) mecanismoDePersistenciaClientesJuridico.recuperaDoArquivo();
            if(clientes == null){
            	clientes = new ArrayList<ClienteJuridico>();
            }
        }catch(Exception e){
            JOptionPane.showMessageDialog(null, "Houve um problema no carregamento do banco de dados do Sistema.", null, JOptionPane.ERROR_MESSAGE);
        }
    }

    @SuppressWarnings("unchecked")
	private void persistirDados(){
        try{
           geradorDeLog.escreveLog("Gravando dados");
           mecanismoDePersistenciaClientesJuridico.persisteNoArquivo((ArrayList) this.clientes);
        }catch(Exception e){
        }
    }

    
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTextField6 = new javax.swing.JTextField();
        jTextField1 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jTextField13 = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jTextField3 = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jTextField4 = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jTextField5 = new javax.swing.JTextField();
        jSeparator1 = new javax.swing.JSeparator();
        jSeparator2 = new javax.swing.JSeparator();
        jLabel7 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jTextField7 = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jTextField8 = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        jTextField9 = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        jTextField10 = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        jComboBox2 = new javax.swing.JComboBox();
        jLabel12 = new javax.swing.JLabel();
        jTextField11 = new javax.swing.JTextField();
        jLabel13 = new javax.swing.JLabel();
        jTextField12 = new javax.swing.JTextField();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        scrollPane1 = new java.awt.ScrollPane();
        list1 = new java.awt.List();
        jLabel14 = new javax.swing.JLabel();
        jLabel15 = new javax.swing.JLabel();
        jSeparator3 = new javax.swing.JSeparator();
        jSeparator4 = new javax.swing.JSeparator();

        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });

        jButton1.setText("OK");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel1.setText("Nome de Fantasia:");

        jLabel2.setText("Razao Social:");

        jLabel3.setText("CNPJ:");

        jLabel4.setText("Inscricao Estadual:");

        jLabel5.setText("Telefone:");

        jLabel6.setText("Email:");

        jLabel7.setText("Endereco");

        jLabel8.setText("No.:");

        jLabel9.setText("Bairro:");

        jLabel10.setText("Cidade:");

        jLabel11.setText("Estado:");

        jLabel12.setText("Observacao:");

        jLabel13.setText("CEP:");

        jButton2.setText("Limpar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Excluir");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText("Cadastrar");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jButton5.setText("Salvar");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jButton6.setText("Voltar");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        list1.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                list1ItemStateChanged(evt);
            }
        });
        scrollPane1.add(list1);
        list1.setPreferredSize(new java.awt.Dimension(200, 297));

        jLabel14.setText("Consultar:");

        jLabel15.setFont(new java.awt.Font("DejaVu Sans", 1, 14));
        jLabel15.setText("Clientes Pessoa Juridica");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setVerticalGroup(layout.createSequentialGroup()
        	.addContainerGap()
        	.addGroup(layout.createParallelGroup()
        	    .addGroup(GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
        	        .addComponent(jSeparator3, GroupLayout.PREFERRED_SIZE, 10, GroupLayout.PREFERRED_SIZE)
        	        .addGap(6))
        	    .addComponent(jLabel15, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
        	.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
        	.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
        	    .addComponent(jTextField13, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
        	    .addComponent(jLabel14, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
        	    .addComponent(jButton1, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
        	    .addComponent(jLabel1, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
        	    .addComponent(jTextField1, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
        	.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
        	.addGroup(layout.createParallelGroup()
        	    .addGroup(GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
        	        .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 300, GroupLayout.PREFERRED_SIZE)
        	        .addGap(0, 19, Short.MAX_VALUE))
        	    .addGroup(layout.createSequentialGroup()
        	        .addGap(38)
        	        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
        	            .addComponent(jTextField6, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
        	            .addComponent(jLabel2, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
        	        .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
        	        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
        	            .addComponent(jTextField2, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
        	            .addComponent(jLabel3, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
        	            .addComponent(jLabel4, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
        	            .addComponent(jTextField3, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
        	        .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
        	        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
        	            .addComponent(jTextField4, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
        	            .addComponent(jLabel5, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
        	            .addComponent(jLabel6, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
        	            .addComponent(jTextField5, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
        	        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
        	        .addGroup(layout.createParallelGroup()
        	            .addGroup(GroupLayout.Alignment.LEADING, layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
        	                .addComponent(jSeparator1, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 10, GroupLayout.PREFERRED_SIZE)
        	                .addComponent(jSeparator2, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 10, GroupLayout.PREFERRED_SIZE))
        	            .addComponent(jLabel7, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
        	        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
        	        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
        	            .addComponent(jTextField8, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
        	            .addComponent(jLabel8, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
        	            .addComponent(jTextField7, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
        	            .addComponent(jComboBox1, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
        	        .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
        	        .addGroup(layout.createParallelGroup()
        	            .addGroup(GroupLayout.Alignment.LEADING, layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
        	                .addComponent(jTextField9, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
        	                .addComponent(jLabel11, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
        	                .addComponent(jLabel10, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
        	                .addComponent(jTextField10, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
        	                .addComponent(jLabel9, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
        	            .addGroup(GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
        	                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 0, Short.MAX_VALUE)
        	                .addComponent(jComboBox2, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)))
        	        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 0, GroupLayout.PREFERRED_SIZE)
        	        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
        	            .addComponent(jTextField11, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
        	            .addComponent(jLabel12, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
        	        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 0, GroupLayout.PREFERRED_SIZE)
        	        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
        	            .addComponent(jTextField12, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
        	            .addComponent(jLabel13, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
        	        .addGap(0, 33, GroupLayout.PREFERRED_SIZE)
        	        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
        	            .addComponent(jButton2, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
        	            .addComponent(jButton3, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
        	            .addComponent(jButton4, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
        	            .addComponent(jButton5, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
        	            .addComponent(jButton6, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))))
        	.addGap(32)
        	.addComponent(jSeparator4, GroupLayout.PREFERRED_SIZE, 10, GroupLayout.PREFERRED_SIZE));
        layout.setHorizontalGroup(layout.createSequentialGroup()
        	.addContainerGap()
        	.addGroup(layout.createParallelGroup()
        	    .addGroup(layout.createSequentialGroup()
        	        .addGroup(layout.createParallelGroup()
        	            .addGroup(layout.createSequentialGroup()
        	                .addGroup(layout.createParallelGroup()
        	                    .addComponent(jSeparator1, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 50, GroupLayout.PREFERRED_SIZE)
        	                    .addGroup(GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
        	                        .addComponent(jLabel9, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
        	                        .addGap(9)))
        	                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
        	                .addGroup(layout.createParallelGroup()
        	                    .addGroup(GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
        	                        .addComponent(jTextField9, GroupLayout.PREFERRED_SIZE, 107, GroupLayout.PREFERRED_SIZE)
        	                        .addGap(37))
        	                    .addComponent(jTextField2, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 144, GroupLayout.PREFERRED_SIZE)))
        	            .addGroup(GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
        	                .addComponent(jLabel3, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
        	                .addGap(8)
        	                .addComponent(jTextField12, GroupLayout.PREFERRED_SIZE, 120, GroupLayout.PREFERRED_SIZE)
        	                .addGap(40))
        	            .addGroup(GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
        	                .addComponent(jLabel15, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
        	                .addGap(12)))
        	        .addGap(10)
        	        .addGroup(layout.createParallelGroup()
        	            .addGroup(GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
        	                .addComponent(jButton4, GroupLayout.PREFERRED_SIZE, 86, GroupLayout.PREFERRED_SIZE)
        	                .addGap(33))
        	            .addComponent(jLabel4, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
        	        .addGap(0, 18, Short.MAX_VALUE)
        	        .addGroup(layout.createParallelGroup()
        	            .addComponent(jTextField5, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 109, GroupLayout.PREFERRED_SIZE)
        	            .addComponent(jTextField3, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 109, GroupLayout.PREFERRED_SIZE))
        	        .addGap(368))
        	    .addGroup(GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
        	        .addComponent(jLabel5, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
        	        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
        	        .addGroup(layout.createParallelGroup()
        	            .addGroup(GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
        	                .addComponent(jLabel7, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
        	                .addGap(8)
        	                .addGroup(layout.createParallelGroup()
        	                    .addComponent(jSeparator2, GroupLayout.Alignment.LEADING, 0, 321, Short.MAX_VALUE)
        	                    .addComponent(jTextField1, GroupLayout.Alignment.LEADING, 0, 321, Short.MAX_VALUE)))
        	            .addGroup(GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
        	                .addGap(8)
        	                .addComponent(jTextField4, GroupLayout.PREFERRED_SIZE, 190, GroupLayout.PREFERRED_SIZE)
        	                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
        	                .addComponent(jLabel6, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
        	                .addGap(12)
        	                .addComponent(jButton5, GroupLayout.PREFERRED_SIZE, 81, GroupLayout.PREFERRED_SIZE)
        	                .addGap(0, 49, Short.MAX_VALUE)))
        	        .addGap(371))
        	    .addGroup(layout.createSequentialGroup()
        	        .addGroup(layout.createParallelGroup()
        	            .addComponent(jLabel1, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
        	            .addGroup(GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
        	                .addGroup(layout.createParallelGroup()
        	                    .addComponent(jLabel13, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
        	                    .addComponent(jComboBox1, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
        	                .addGap(0, 0, Short.MAX_VALUE)
        	                .addComponent(jLabel12, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
        	                .addGap(12)))
        	        .addGroup(layout.createParallelGroup()
        	            .addComponent(jTextField11, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 335, GroupLayout.PREFERRED_SIZE)
        	            .addComponent(jTextField6, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 335, GroupLayout.PREFERRED_SIZE))
        	        .addGap(371))
        	    .addComponent(jSeparator4, GroupLayout.Alignment.LEADING, 0, 824, Short.MAX_VALUE)
        	    .addGroup(layout.createSequentialGroup()
        	        .addGroup(layout.createParallelGroup()
        	            .addGroup(GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
        	                .addPreferredGap(jLabel13, jButton2, LayoutStyle.ComponentPlacement.INDENT)
        	                .addComponent(jButton2, GroupLayout.PREFERRED_SIZE, 75, GroupLayout.PREFERRED_SIZE))
        	            .addGroup(GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
        	                .addComponent(jLabel2, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
        	                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)))
        	        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
        	        .addGroup(layout.createParallelGroup()
        	            .addGroup(GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
        	                .addComponent(jButton3, GroupLayout.PREFERRED_SIZE, 78, GroupLayout.PREFERRED_SIZE)
        	                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
        	                .addComponent(jLabel10, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
        	                .addGroup(layout.createParallelGroup()
        	                    .addGroup(GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
        	                        .addComponent(jTextField10, GroupLayout.PREFERRED_SIZE, 138, GroupLayout.PREFERRED_SIZE)
        	                        .addGap(0, 464, Short.MAX_VALUE))
        	                    .addGroup(GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
        	                        .addGap(0, 18, Short.MAX_VALUE)
        	                        .addComponent(jSeparator3, GroupLayout.PREFERRED_SIZE, 584, GroupLayout.PREFERRED_SIZE))))
        	            .addGroup(GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
        	                .addComponent(jTextField7, GroupLayout.PREFERRED_SIZE, 248, GroupLayout.PREFERRED_SIZE)
        	                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
        	                .addComponent(jLabel8, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
        	                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
        	                .addGroup(layout.createParallelGroup()
        	                    .addGroup(GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
        	                        .addComponent(jLabel11, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
        	                        .addGap(12)
        	                        .addGroup(layout.createParallelGroup()
        	                            .addComponent(jButton6, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 79, GroupLayout.PREFERRED_SIZE)
        	                            .addGroup(GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
        	                                .addComponent(jComboBox2, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
        	                                .addGap(54))))
        	                    .addGroup(GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
        	                        .addPreferredGap(jLabel11, jTextField8, LayoutStyle.ComponentPlacement.INDENT)
        	                        .addComponent(jTextField8, GroupLayout.PREFERRED_SIZE, 69, GroupLayout.PREFERRED_SIZE)
        	                        .addGap(58)))
        	                .addGap(0, 36, Short.MAX_VALUE)
        	                .addGroup(layout.createParallelGroup()
        	                    .addComponent(scrollPane1, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 277, GroupLayout.PREFERRED_SIZE)
        	                    .addGroup(GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
        	                        .addComponent(jLabel14, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
        	                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 0, GroupLayout.PREFERRED_SIZE)
        	                        .addComponent(jTextField13, GroupLayout.PREFERRED_SIZE, 165, GroupLayout.PREFERRED_SIZE)
        	                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
        	                        .addComponent(jButton1, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)))))))
        	.addContainerGap());
        this.setPreferredSize(new java.awt.Dimension(848, 422));
    }// </editor-fold>//GEN-END:initComponents

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
        
}//GEN-LAST:event_jTextField1ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        String parametroDeConsulta = jTextField13.getText();

        String[] consultados = list1.getItems();

        for ( int i=0; i<consultados.length; i++ ){
            if ( (consultados[i].toLowerCase()).
                    contains(parametroDeConsulta.toLowerCase()) ){
                list1.select(i);
                jTextField6.setText("");
                selecionarCliente();
                break;
            }
        }
}//GEN-LAST:event_jButton1ActionPerformed

    private void limparCampos(){
        jTextField1.setText("");
        jTextField2.setText("");
        jTextField3.setText("");
        jTextField4.setText("");
        jTextField5.setText("");
        jTextField6.setText("");
        jTextField7.setText("");
        jTextField8.setText("");
        jTextField9.setText("");
        jTextField10.setText("");
        jTextField11.setText("");
        jTextField12.setText("");
        jLabel1.setForeground(Color.BLACK);
        jLabel2.setForeground(Color.BLACK);
        jLabel3.setForeground(Color.BLACK);
        jLabel4.setForeground(Color.BLACK);
        jLabel5.setForeground(Color.BLACK);
        jLabel6.setForeground(Color.BLACK);
        jLabel8.setForeground(Color.BLACK);
        jLabel9.setForeground(Color.BLACK);
        jLabel10.setForeground(Color.BLACK);
        jLabel11.setForeground(Color.BLACK);
        jLabel12.setForeground(Color.BLACK);
        jLabel13.setForeground(Color.BLACK);
    }

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        limparCampos();
        list1.select(-1);
        jButton5.setEnabled(false);
        jButton4.setEnabled(true);
        jButton3.setEnabled(false);
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        limparCampos();
        if ( clienteAtual != null ){
            list1.remove(clienteAtual.getNomeFantasia()+" # "+clienteAtual.getCNPJ());
            clientes.remove(clienteAtual);
            try {
				geradorDeLog.escreveLog("O registro do cliente "+clienteAtual.getNomeFantasia()+" de CNPJ: "+clienteAtual.getCNPJ()+" foi excluido.");
			} catch (Exception e) {
				
			}
            clienteAtual = null;
            jButton5.setEnabled(false);
            jButton4.setEnabled(true);
            jButton3.setEnabled(false);
            list1.select(-1);
            JOptionPane.showMessageDialog(null, "Cliente excluido com sucesso!", "Cliente removido", JOptionPane.PLAIN_MESSAGE);
            persistirDados();
        } else{
            JOptionPane.showMessageDialog(null, "Voce deve selecionar um cliente para ser excluido!", "Cliente nao especificado", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_jButton3ActionPerformed

    private void cadastraCliente(ClienteJuridico cliente, TipoDeCadastro tipoDeCadastro, String mensagemSucesso, String tituloSucesso, String mensagemFalha, String tituloFalha) {
        jLabel1.setForeground(Color.BLACK);
        jLabel2.setForeground(Color.BLACK);
        jLabel3.setForeground(Color.BLACK);
        jLabel4.setForeground(Color.BLACK);
        jLabel5.setForeground(Color.BLACK);
        jLabel6.setForeground(Color.BLACK);
        jLabel8.setForeground(Color.BLACK);
        jLabel9.setForeground(Color.BLACK);
        jLabel10.setForeground(Color.BLACK);
        jLabel11.setForeground(Color.BLACK);
        jLabel12.setForeground(Color.BLACK);
        jLabel13.setForeground(Color.BLACK);

        List<TipoDeEntradaInvalida> entradasInvalidas = new ArrayList<TipoDeEntradaInvalida>();
 
        try {
            cliente.setNomeFantasia(jTextField1.getText());
        } catch (Exception e) {
            entradasInvalidas.add(TipoDeEntradaInvalida.NOME_FANTASIA);
        }

        try {
            cliente.setTelefone(jTextField4.getText());
        } catch (Exception e) {
            entradasInvalidas.add(TipoDeEntradaInvalida.TELEFONE);
        }

        try {
            cliente.setEmail(jTextField5.getText());
        } catch (Exception e) {
            entradasInvalidas.add(TipoDeEntradaInvalida.EMAIL);
        }

        String CNPJ = jTextField2.getText();
        boolean cnpjValido = true;
        try {
            for (ClienteJuridico clienteJuridico : clientes) {
                if (CNPJ.equals(clienteJuridico.getCNPJ())) {
                    cnpjValido = false;
                }
            }
            if (cnpjValido) {
                cliente.setCNPJ(CNPJ);
            } else {
                throw new Exception();
            }
        } catch (Exception e) {
            entradasInvalidas.add(TipoDeEntradaInvalida.CNPJ);
        }

        String inscricaoEstadual = jTextField3.getText();
        boolean inscEstadualValido = true;
        try {
            for (ClienteJuridico clienteJuridico : clientes) {
                if (inscricaoEstadual.equals(clienteJuridico.getInscricaoEstadual())) {
                    inscEstadualValido = false;
                }
            }
            if (inscEstadualValido) {
                cliente.setInscricaoEstadual(inscricaoEstadual);
            }
            else{
                throw new Exception();
            }
        } catch (Exception e) {
            entradasInvalidas.add(TipoDeEntradaInvalida.INSCRICAO_ESTADUAL);
        }

        try {
            cliente.setRazaoSocial(jTextField6.getText());
        } catch (Exception e) {
            entradasInvalidas.add(TipoDeEntradaInvalida.RAZAO_SOCIAL);
        }

        Endereco endereco = new Endereco();
        try {
            endereco.setTipoDeLogradouro((TipoDeLogradouro) jComboBox1.getSelectedItem());
        } catch (Exception e) {
            entradasInvalidas.add(TipoDeEntradaInvalida.TIPO_DE_LOGRADOURO);
        }
        try {
            endereco.setLogradouro(jTextField7.getText());
        } catch (Exception e) {
            entradasInvalidas.add(TipoDeEntradaInvalida.LOGRADOURO);
        }
        try {
            endereco.setNumero(Integer.parseInt(jTextField8.getText()));
        } catch (Exception e) {
            entradasInvalidas.add(TipoDeEntradaInvalida.NUMERO_ENDERECO);
        }
        try {
            endereco.setObs(jTextField11.getText());
        } catch (Exception e) {
            entradasInvalidas.add(TipoDeEntradaInvalida.OBSERVACAO);
        }
        try {
            endereco.setBairro(jTextField9.getText());
        } catch (Exception e) {
            entradasInvalidas.add(TipoDeEntradaInvalida.BAIRRO);
        }
        try {
            endereco.setCidade(jTextField10.getText());
        } catch (Exception e) {
            entradasInvalidas.add(TipoDeEntradaInvalida.CIDADE);
        }
        try {
            endereco.setEstado((UF) jComboBox2.getSelectedItem());
        } catch (Exception e) {
            entradasInvalidas.add(TipoDeEntradaInvalida.ESTADO);
        }
        try {
            endereco.setCep(jTextField12.getText());
        } catch (Exception e) {
            entradasInvalidas.add(TipoDeEntradaInvalida.CEP);
        }

        try {
            cliente.setEndereco(endereco);
        } catch (Exception e) {
            entradasInvalidas.add(TipoDeEntradaInvalida.ENDERECO);
        }

        if (entradasInvalidas.size() == 0) {
            clientes.add(cliente);
            try {
				geradorDeLog.escreveLog("Cliente Juridico "+cliente.getNomeFantasia()+" de CNPJ: "+cliente.getCNPJ()+" foi cadastrado.");
			} catch (Exception e) {
				
			}
            list1.add(cliente.getNomeFantasia() + " # " + cliente.getCNPJ());
            list1.select(list1.getItemCount()-1);
            JOptionPane.showMessageDialog(null, mensagemSucesso, tituloSucesso, JOptionPane.PLAIN_MESSAGE);
            list1.select(-1);
            limparCampos();
        } else {
            if (tipoDeCadastro == TipoDeCadastro.EDICAO) {
                list1.add(cliente.getNomeFantasia() + " # " + cliente.getCNPJ());
                clientes.add(cliente);
                try {
    				geradorDeLog.escreveLog("As informacoes do cliente juridico "+cliente.getNomeFantasia()+" de CNPJ: "+cliente.getCNPJ()+" foram editadas");
    			} catch (Exception e) {
    				
    			}
                list1.select(list1.getItemCount()-1);
            }
            if (!cnpjValido || !inscEstadualValido) {
                JOptionPane.showMessageDialog(null, "Nao pode haver duplicacao de CNPJ ou inscricoes estaduais.", tituloFalha, JOptionPane.ERROR_MESSAGE);
            }
            else{
                JOptionPane.showMessageDialog(null, mensagemFalha, tituloFalha,JOptionPane.ERROR_MESSAGE);
            }
            for (TipoDeEntradaInvalida entradaInvalida : entradasInvalidas) {
                if (entradaInvalida == TipoDeEntradaInvalida.NOME_FANTASIA) {
                    jLabel1.setForeground(Color.RED);
                } else if (entradaInvalida == TipoDeEntradaInvalida.TELEFONE) {
                    jLabel5.setForeground(Color.RED);
                    jTextField4.setText("(XX) XXXX-XXXX");
                } else if (entradaInvalida == TipoDeEntradaInvalida.EMAIL) {
                    jLabel6.setForeground(Color.RED);
                } else if (entradaInvalida == TipoDeEntradaInvalida.CNPJ) {
                    jLabel3.setForeground(Color.RED);
                    jTextField2.setText("XXXXXXXXXXXXXX (14)");
                } else if (entradaInvalida == TipoDeEntradaInvalida.INSCRICAO_ESTADUAL) {
                    jLabel4.setForeground(Color.RED);
                    jTextField3.setText("XXXXXXXXXXXX (12)");
                } else if (entradaInvalida == TipoDeEntradaInvalida.RAZAO_SOCIAL) {
                    jLabel2.setForeground(Color.RED);
                } else if (entradaInvalida == TipoDeEntradaInvalida.NUMERO_ENDERECO) {
                    jLabel8.setForeground(Color.RED);
                } else if (entradaInvalida == TipoDeEntradaInvalida.BAIRRO) {
                    jLabel9.setForeground(Color.RED);
                } else if (entradaInvalida == TipoDeEntradaInvalida.CIDADE) {
                    jLabel10.setForeground(Color.RED);
                } else if (entradaInvalida == TipoDeEntradaInvalida.ESTADO) {
                    jLabel11.setForeground(Color.RED);
                } else if (entradaInvalida == TipoDeEntradaInvalida.OBSERVACAO) {
                    jLabel12.setForeground(Color.RED);
                } else if (entradaInvalida == TipoDeEntradaInvalida.CEP) {
                    jLabel13.setForeground(Color.RED);
                    jTextField12.setText("XXXXXXXX");
                }
            }
        }
    }

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        ClienteJuridico novo = new ClienteJuridico();
        cadastraCliente(novo, TipoDeCadastro.NOVA_ENTRADA, "Cadastro efetuado com sucesso.", "Cadastro realizado", "O cadastro falhou. Os campos errados estao em destaque.", "Cadastro falhou");
        persistirDados();
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        list1.remove(clienteAtual.getNomeFantasia()+" # "+clienteAtual.getCNPJ());
        clientes.remove(clienteAtual);
        cadastraCliente(clienteAtual, TipoDeCadastro.EDICAO,"Edicao realizada com sucesso.", "Edicao realizada", "A edicao falhou. Verifique campos em destaque.", "Edicao nao realizada");
        jButton5.setEnabled(false);
        jButton4.setEnabled(false);
        jButton3.setEnabled(true);
        persistirDados();
    }//GEN-LAST:event_jButton5ActionPerformed

    private void list1ItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_list1ItemStateChanged
        selecionarCliente();
    }//GEN-LAST:event_list1ItemStateChanged

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        MainGrafico.entrarNaArea(MainGrafico.getMenuCorrente(), Constantes.INTERFACE_TAMANHO_MENU[0], Constantes.INTERFACE_TAMANHO_MENU[1]);
        MainGrafico.sairDaArea(this);
    }//GEN-LAST:event_jButton6ActionPerformed

    private void selecionarCliente( ){
        limparCampos();
        String selecionado = list1.getItems()[list1.getSelectedIndex()];
        String CNPJDoCliente = selecionado.split(" # ")[1];
        for (ClienteJuridico cliente : clientes) {
            if (cliente.getCNPJ().replace(".", "").replace("-", "").equals(CNPJDoCliente.replace(".", "").replace("-", ""))) {
                clienteAtual = cliente;
                jButton5.setEnabled(true);
                jButton4.setEnabled(false);
                jButton3.setEnabled(true);
                jTextField1.setText(cliente.getNomeFantasia());
                jTextField4.setText(cliente.getTelefone());
                jTextField5.setText(cliente.getEmail());
                jTextField2.setText(cliente.getCNPJ());
                jTextField3.setText(cliente.getInscricaoEstadual());
                jTextField6.setText(cliente.getRazaoSocial());
                jComboBox1.setSelectedItem(cliente.getEndereco().getTipoDeLogradouro());
                jTextField7.setText(cliente.getEndereco().getLogradouro());
                jTextField8.setText(new Integer(cliente.getEndereco().getNumero()).toString());
                jTextField9.setText(cliente.getEndereco().getBairro());
                jTextField10.setText(cliente.getEndereco().getCidade());
                jComboBox2.setSelectedItem(cliente.getEndereco().getEstado());
                jTextField11.setText(cliente.getEndereco().getObs());
                jTextField12.setText(cliente.getEndereco().getCep());
                break;
            }
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JSeparator jSeparator4;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField10;
    private javax.swing.JTextField jTextField11;
    private javax.swing.JTextField jTextField12;
    private javax.swing.JTextField jTextField13;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JTextField jTextField5;
    private javax.swing.JTextField jTextField6;
    private javax.swing.JTextField jTextField7;
    private javax.swing.JTextField jTextField8;
    private javax.swing.JTextField jTextField9;
    private java.awt.List list1;
    private java.awt.ScrollPane scrollPane1;
    // End of variables declaration//GEN-END:variables

}
